<template>
	<div class="padding20">
		<el-row style="margin-bottom: 7px">
			<el-button type="primary" size="medium" icon="el-icon-plus" v-if="$tools.hasPermission('study:course:studyCourseClassify:save')" @click="handleEdit()">
				添加
			</el-button>
			<el-button type="primary" size="medium" icon="el-icon-arrow-down" @click="onClickUnfold">全部展开</el-button>
			<el-button type="primary" size="medium" icon="el-icon-arrow-up" @click="onClickFold">全部折叠</el-button>
			<el-button v-if="$tools.hasPermission('study:course:studyCourseClassify:deleteBatch')" size="medium" type="danger" icon="el-icon-delete" @click="handleDeleteBatch()">
				删除
			</el-button>
			<div style="float: right; padding-right: 17px">
				<!--                <i-->
				<!--                  class="el-icon-download"-->
				<!--                  style="margin-right: 10px; cursor: pointer"-->
				<!--                  @click="handleExport"-->
				<!--                ></i>-->
				<i class="el-icon-refresh" style="margin-right: 10px; cursor: pointer" @click="refresh"></i>

				<el-popover placement="bottom" width="180" trigger="click" title="列展示" popper-class="columnShows">
					<el-checkbox-group v-model="tableShowList">
						<el-checkbox v-for="item in tableList" :label="item.propName" :key="item.propName" :value="item.propName">
							{{ item.propDesc }}
						</el-checkbox>
					</el-checkbox-group>
					<i slot="reference" class="el-icon-s-operation" style="cursor: pointer"></i>
				</el-popover>
			</div>
		</el-row>
		<el-table v-if="menuTable" ref="multipleTable" :data="tableData" style="width: 100%; margin-bottom: 20px" row-key="id" :default-expand-all="isFold" border :tree-props="{ children: 'children' }">
			<el-table-column v-if="$tools.isShow('name', tableShowList)" prop="name" label="名称" :show-overflow-tooltip="true"></el-table-column>
			<!-- <el-table-column v-if="$tools.isShow('name', tableShowList)" prop="status" label="状态" :show-overflow-tooltip="true">
				<template #default="{row}">
					<el-switch
						v-model="row.status"
						active-color="#13ce66"
						inactive-color="#ff4949"
						:active-value="1"
						:inactive-value="2"
						active-text="启用"
						inactive-text="禁用"
						@change="onStatusChange($event, row)"
					></el-switch>
				</template>
			</el-table-column> -->
			<el-table-column v-if="$tools.isShow('sort', tableShowList)" prop="sort" label="排序" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column label="操作" fixed="right" min-width="180">
				<template slot-scope="scope">
					<el-button v-if="$tools.hasPermission('study:course:studyCourseClassify:info')" size="mini" plain @click="handleInfo(scope.row.id)">
						详情
					</el-button>
					<el-button v-if="$tools.hasPermission('study:course:studyCourseClassify:edit')" size="mini" plain @click="handleEdit(scope.row.id)">
						编辑
					</el-button>
					<el-button v-if="$tools.hasPermission('study:course:studyCourseClassify:delete')" size="mini" plain type="danger" @click="handleDelete(scope.row)">
						删除
					</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!-- 新增、编辑 -->
		<form-edit v-if="formEditVisible" ref="formEdit" :allParents="tableData" @refreshData="queryList"></form-edit>
		<!-- 详情 -->
		<form-info v-if="formInfoVisible" ref="formInfo"></form-info>
	</div>
</template>
<script>
import { studycourseclassifyTree, studycourseclassifyDel, studycourseclassifyDelBatch, studycourseclassifyDynamicExport } from "@/api/study/course/studycourseclassify.js"
import { viewTableList } from "@/api/public.api.js"
import FormEdit from "./studycourseclassify-edit"
import FormInfo from "./studycourseclassify-info"
import { enumList } from "@/api/public.api.js"
import { studycourseclassifyEdit } from "@/api/study/course/studycourseclassify.js"

export default {
	data() {
		return {
			tableData: [],
			formEditVisible: false,
			formInfoVisible: false,
			queryForm: {
				name: "",
				status: "",
			},
			collapse: {
				icon: "el-icon-arrow-down el-icon--right",
				label: "展开",
				flag: false,
			},
			tableList: [],
			tableShowList: [],
			YesNoList: [],
			isFold: false,
			menuTable: true,
			defaultExpandIds: [],
		}
	},
	components: {
		"form-edit": FormEdit,
		"form-info": FormInfo,
	},
	created() {
		this.queryEnumList()
		this.initTableList()
		this.queryList()
	},
	methods: {
		onStatusChange(status, row) {
			studycourseclassifyEdit({
				...row,
				status,
			}).then(resp => {
				if (resp.code == 0) {
					this.$message({
						type: "success",
						message: "操作成功!",
					})
				} else {
					this.$msg({
						message: resp.msg,
						type: "error",
					})
				}
			})
		},
		queryEnumList() {
			this.YesNoList = []
			enumList("YesNo", "").then(resp => {
				if (resp.code == 0) {
					this.YesNoList = resp.data
				}
			})
		},
		refresh() {
			this.tableData = []
			this.queryList({ current: 1 })
		},
		//查询表格数据
		queryList() {
			studycourseclassifyTree(this.queryForm).then(resp => {
				if (resp.code == 0) {
					this.tableData = resp.data
				} else {
					this.$msg({
						message: resp.msg,
						type: "error",
					})
				}
			})
		},
		//打开编辑窗口
		handleEdit(id) {
			this.formEditVisible = true
			this.$nextTick(() => {
				this.$refs.formEdit.init(id)
			})
		},
		//打开详情窗口
		handleInfo(id) {
			this.formInfoVisible = true
			this.$nextTick(() => {
				this.$refs.formInfo.init(id)
			})
		},
		//删除
		handleDelete(row) {
			this.$confirm("确认删除该记录及子数据及绑定的资源, 是否继续?", "提示", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning",
			})
				.then(() => {
					studycourseclassifyDel(row.id).then(resp => {
						if (resp.code == 0) {
							this.$message({
								type: "success",
								message: "操作成功!",
							})
							this.queryList()
						} else {
							this.$msg({
								message: resp.msg,
								type: "error",
							})
						}
					})
				})
				.catch(() => {})
		},
		//批量删除
		handleDeleteBatch() {
			let checkedRow = this.$refs.multipleTable.selection
			if (checkedRow.length > 0) {
				let ids = []
				checkedRow.forEach(item => {
					ids.push(item.id)
				})
				this.$confirm("确认删除该记录及子数据, 是否继续?", "提示", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning",
				})
					.then(() => {
						studycourseclassifyDelBatch(ids).then(resp => {
							if (resp.code == 0) {
								this.$message({
									type: "success",
									message: "操作成功!",
								})
								this.queryList()
							} else {
								this.$msg({
									message: resp.msg,
									type: "error",
								})
							}
						})
					})
					.catch(() => {})
			} else {
				this.$msg({
					message: "请选择数据进行删除",
					type: "error",
				})
			}
		},
		//重置查询框
		resetQueryForm() {
			this.queryForm = {
				name: "",
				status: "",
			}
			this.queryList({ current: 1 })
		},
		//切换查询条件收缩框
		handleCollapseState() {
			this.collapse.flag = !this.collapse.flag
			if (this.collapse.flag) {
				this.collapse.icon = "el-icon-arrow-up el-icon--right"
				this.collapse.label = "收起"
			} else {
				this.collapse.icon = "el-icon-arrow-down el-icon--right"
				this.collapse.label = "展开"
			}
		},
		//视图表格列表初始化
		initTableList() {
			viewTableList("study_course_classify").then(resp => {
				if (resp.code == 0) {
					this.tableList = resp.data
					this.tableList.forEach(item => {
						this.tableShowList.push(item.propName)
					})
				}
			})
		},
		statusFmt(row) {
			let name = "禁用"
			if (row.status == 1) {
				name = "启用"
			}
			return name
		},
		//可见字段excel导出
		handleExport() {
			if (!this.tableShowList || this.tableShowList.length < 1) {
				this.$msg({
					message: "请选择要导出的列",
					type: "error",
				})
				return
			}
			let colNum = 1
			let headerList = []
			this.tableShowList.forEach(item => {
				let nameData = this.tableList.find(item2 => {
					return item2.propName == item
				})
				let name = ""
				if (nameData && nameData.propDesc) {
					name = nameData.propDesc
				} else {
					name = "未定义字段" + colNum++
				}
				headerList.push({ name: name, code: item })
			})
			studycourseclassifyDynamicExport({ headerList: headerList, dto: this.queryForm })
		},
		onClickUnfold() {
			this.isFold = true
		},
		onClickFold() {
			this.isFold = false
		},
	},
	watch: {
		isFold() {
			this.menuTable = false
			this.$nextTick(() => {
				this.menuTable = true
			})
		},
	},
}
</script>
<style scoped></style>
